package com.snail.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.snail.model.dto.LoginResultDTO;
import com.snail.model.pojo.SysUser;
import com.snail.response.BaseResponse;
import com.snail.response.ResultUtils;
import com.snail.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 蔡徐坤
 * @Date 2025/7/14 23:42
 * @Description: 用于实现登录和注册功能
 */

@RestController
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户注册功能
     */

    @PostMapping("/register")
    public BaseResponse<String> register(@RequestBody SysUser sysUser) {
        sysUserService.register(sysUser);
        return ResultUtils.success("注册成功");
    }

    /**
     * 用户登录功能
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/user/login")
    public BaseResponse<LoginResultDTO> userLogin(@RequestBody SysUser sysUser) {
        LoginResultDTO result = sysUserService.userLogin(sysUser);
        return ResultUtils.success(result);
    }


    /**
     * 管理员登录功能
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/admin/login")
    public BaseResponse<LoginResultDTO> adminLogin(@RequestBody SysUser sysUser) {
        LoginResultDTO result = sysUserService.adminLogin(sysUser);
        return ResultUtils.success(result);
    }

    /**
     * 退出登录功能，清楚登录用户的sa-token
     */
    @PostMapping("/logout")
    public BaseResponse<String> logout() {
        StpUtil.logout();
        return ResultUtils.success("退出登录成功");
    }
}
